package com.gxuwz.weather.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.gxuwz.weather.R;
import com.gxuwz.weather.model.WeatherDay2and15;
import com.gxuwz.weather.model.Weatherhour3;
import com.gxuwz.weather.model.Weathernow;

import java.util.List;

public class WeatherFragment extends Fragment {

    private static final String ARG_DATA = "data";
    private View view;

    public static WeatherFragment newInstance(Weathernow data) {

        WeatherFragment fragment = new WeatherFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_DATA, data);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 加载 fragment_city_weather.xml 布局文件
        view = inflater.inflate(R.layout.fragment_city_weather, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        // 获取传递的数据
        Weathernow data = (Weathernow) getArguments().getSerializable(ARG_DATA);
        if (data == null) {
            Log.e("WeatherFragment", "Weathernow data is null");
            return;
        }

        // 更新 TextView
        TextView fragTvArea = view.findViewById(R.id.frag_tv_area);
        fragTvArea.setText(data.getAreaCn());

        TextView fragTvDate = view.findViewById(R.id.frag_tv_date);
        fragTvDate.setText(data.getDate());

        TextView fragTvCurrentTemp = view.findViewById(R.id.frag_tv_currenttemp);
        fragTvCurrentTemp.setText(data.getTemp() + "℃");

        TextView fragTvCondition = view.findViewById(R.id.frag_tv_condition);
        fragTvCondition.setText(data.getWeather());

        TextView fragTvWind = view.findViewById(R.id.frag_tv_wind);
        fragTvWind.setText(data.getWD() + data.getWS());

        TextView fragTvHumidity = view.findViewById(R.id.frag_tv_humidity);
        fragTvHumidity.setText("湿度:" + data.getSD());

        TextView fragTvAqi = view.findViewById(R.id.frag_tv_aqi);
        fragTvAqi.setText("PM2.5:" + data.getAqi());

        TextView fragTvIssueContent = view.findViewById(R.id.frag_tv_issueContent);
        fragTvIssueContent.setText(data.getIssueContent());

        // 更新生活指数
        TextView fragIndexTvUv = view.findViewById(R.id.frag_index_tv_uv);
        fragIndexTvUv.setText(data.getLifeIndexUv());

        TextView fragIndexTvSportIndex = view.findViewById(R.id.frag_index_tv_sport_index);
        fragIndexTvSportIndex.setText(data.getLifeIndexSport());

        TextView fragIndexTvColdIndex = view.findViewById(R.id.frag_index_tv_cold_index);
        fragIndexTvColdIndex.setText(data.getLifeIndexCold());

        TextView fragIndexTvWashcarIndex = view.findViewById(R.id.frag_index_tv_washcar_index);
        fragIndexTvWashcarIndex.setText(data.getLifeIndexWashcar());

        TextView fragIndexTvAllergyIndex = view.findViewById(R.id.frag_index_tv_allergy_index);
        fragIndexTvAllergyIndex.setText(data.getLifeIndexAllergy());

        TextView fragIndexTvDressIndex = view.findViewById(R.id.frag_index_tv_dress_index);
        fragIndexTvDressIndex.setText(data.getLifeIndexDress());

        // 根据 weathercode 设置图片
        ImageView fragIvToday = view.findViewById(R.id.frag_iv_today);
        setWeatherIcon(fragIvToday, data.getWeatherCode());

        // 更新3小时天气数据
        LinearLayout sceneLayout = view.findViewById(R.id.scene_layout);
        List<Weatherhour3> hour3List = data.getHour3List();
        for (Weatherhour3 hour3 : hour3List) {
            View hourView = LayoutInflater.from(getContext()).inflate(R.layout.weather_item, sceneLayout, false);
            TextView tvHour3Date = hourView.findViewById(R.id.tv_hour3_date);
            ImageView ivHour3Weathercode = hourView.findViewById(R.id.tv_hour3_weathercode);
            TextView tvHour3Temp = hourView.findViewById(R.id.tv_hour3_temp);

            tvHour3Date.setText(hour3.getTime());
            ivHour3Weathercode.setImageResource(getWeatherIconResId(hour3.getWeathercode()));
            tvHour3Temp.setText(hour3.getTemp());

            sceneLayout.addView(hourView);
        }

        // 更新未来2~15天天气情况列表
        LinearLayout futureWeatherLayout = view.findViewById(R.id.frag_center_layout);
        List<WeatherDay2and15> day2and15List = data.getDay2and15List();
        for (WeatherDay2and15 day2and15 : day2and15List) {
            View dayView = LayoutInflater.from (getContext()).inflate(R.layout.item_main_center, futureWeatherLayout, false);
            TextView tvDate = dayView.findViewById(R.id.item_center_tv_date);
            TextView tvCon = dayView.findViewById(R.id.item_center_tv_con);
            ImageView ivWeather = dayView.findViewById(R.id.item_center_iv);
            TextView tvWind = dayView.findViewById(R.id.item_center_tv_wind);
            TextView tvTemp = dayView.findViewById(R.id.item_center_tv_temp);

            tvDate.setText(day2and15.getDate());
            tvCon.setText(day2and15.getWeather());
            ivWeather.setImageResource(getWeatherIconResId(day2and15.getWeatherCode()));
            tvWind.setText(day2and15.getWD() + day2and15.getWS());
            tvTemp.setText(day2and15.getTempmin() + "~" + day2and15.getTempmax());

            futureWeatherLayout.addView(dayView);
        }
    }


    private int getWeatherIconResId(String weatherCode) {
        int resourceId = 0; // 默认资源ID
        if (weatherCode.startsWith("d")) {
            // 白天天气
            switch (weatherCode) {
                case "d00":
                    resourceId = R.drawable.d00; // 晴
                    break;
                case "d01":
                    resourceId = R.drawable.d01; // 多云
                    break;
                case "d02":
                    resourceId = R.drawable.d02; // 阴
                    break;
                case "d03":
                    resourceId = R.drawable.d03; // 阵雨
                    break;
                case "d04":
                    resourceId = R.drawable.d04; // 雷阵雨
                    break;
                case "d05":
                    resourceId = R.drawable.d05; // 雷阵雨伴有冰雹
                    break;
                case "d06":
                    resourceId = R.drawable.d06; // 雨夹雪
                    break;
                case "d07":
                    resourceId = R.drawable.d07; // 小雨
                    break;
                case "d08":
                    resourceId = R.drawable.d08; // 中雨
                    break;
                case "d09":
                    resourceId = R.drawable.d09; // 大雨
                    break;
                case "d10":
                    resourceId = R.drawable.d10; // 暴雨
                    break;
                case "d11":
                    resourceId = R.drawable.d11; // 大暴雨
                    break;
                case "d12":
                    resourceId = R.drawable.d12; // 特大暴雨
                    break;
                case "d13":
                    resourceId = R.drawable.d13; // 阵雪
                    break;
                case "d14":
                    resourceId = R.drawable.d14; // 小雪
                    break;
                case "d15":
                    resourceId = R.drawable.d15; // 中雪
                    break;
                case "d16":
                    resourceId = R.drawable.d16; // 大雪
                    break;
                case "d17":
                    resourceId = R.drawable.d17; // 暴雪
                    break;
                case "d18":
                    resourceId = R.drawable.d18; // 雾
                    break;
                case "d19":
                    resourceId = R.drawable.d19; // 冻雨
                    break;
                case "d20":
                    resourceId = R.drawable.d20; // 沙尘暴
                    break;
                case "d21":
                    resourceId = R.drawable.d21; // 小到中雨
                    break;
                case "d22":
                    resourceId = R.drawable.d22; // 中到大雨
                    break;
                case "d23":
                    resourceId = R.drawable.d23; // 大到暴雨
                    break;
                case "d24":
                    resourceId = R.drawable.d24; // 暴雨到大暴雨
                    break;
                case "d25":
                    resourceId = R.drawable.d25; // 大暴雨到特大暴雨
                    break;
                case "d26":
                    resourceId = R.drawable.d26; // 未知天气
                    break;
                case "d27":
                    resourceId = R.drawable.d27; // 未知天气
                    break;
                case "d28":
                    resourceId = R.drawable.d28; // 未知天气
                    break;
                case "d29":
                    resourceId = R.drawable.d29; // 未知天气
                    break;
                case "d30":
                    resourceId = R.drawable.d30; // 未知天气
                    break;
                case "d31":
                    resourceId = R.drawable.d31; // 未知天气
                    break;
                case "d53":
                    resourceId = R.drawable.d53; // 未知天气
                    break;
                case "d301":
                    resourceId = R.drawable.d301; // 未知天气
                    break;
                case "d302":
                    resourceId = R.drawable.d302; // 未知天气
                    break;
            }
        } else if (weatherCode.startsWith("n")) {
            // 夜间天气
            switch (weatherCode) {
                case "n00":
                    resourceId = R.drawable.n00; // 晴
                    break;
                case "n01":
                    resourceId = R.drawable.n01; // 多云
                    break;
                case "n02":
                    resourceId = R.drawable.n02; // 阴
                    break;
                case "n03":
                    resourceId = R.drawable.n03; // 阵雨
                    break;
                case "n04":
                    resourceId = R.drawable.n04; // 雷阵雨
                    break;
                case "n05":
                    resourceId = R.drawable.n05; // 雷阵雨伴有冰雹
                    break;
                case "n06":
                    resourceId = R.drawable.n06; // 雨夹雪
                    break;
                case "n07":
                    resourceId = R.drawable.n07; // 小雨
                    break;
                case "n08":
                    resourceId = R.drawable.n08; // 中雨
                    break;
                case "n09":
                    resourceId = R.drawable.n09; // 大雨
                    break;
                case "n10":
                    resourceId = R.drawable.n10; // 暴雨
                    break;
                case "n11":
                    resourceId = R.drawable.n11; // 大暴雨
                    break;
                case "n12":
                    resourceId = R.drawable.n12; // 特大暴雨
                    break;
                case "n13":
                    resourceId = R.drawable.n13; // 阵雪
                    break;
                case "n14":
                    resourceId = R.drawable.n14; // 小雪
                    break;
                case "n15":
                    resourceId = R.drawable.n15; // 中雪
                    break;
                case "n16":
                    resourceId = R.drawable.n16; // 大雪
                    break;
                case "n17":
                    resourceId = R.drawable.n17; // 暴雪
                    break;
                case "n18":
                    resourceId = R.drawable.n18; // 雾
                    break;
                case "n19":
                    resourceId = R.drawable.n19; // 冻雨
                    break;
                case "n20":
                    resourceId = R.drawable.n20; // 沙尘暴
                    break;
                case "n21":
                    resourceId = R.drawable.n21; // 小到中雨
                    break;
                case "n22":
                    resourceId = R.drawable.n22; // 中到大雨
                    break;
                case "n23":
                    resourceId = R.drawable.n23; // 大到暴雨
                    break;
                case "n24":
                    resourceId = R.drawable.n24; // 暴雨到大暴雨
                    break;
                case "n25":
                    resourceId = R.drawable.n25; // 大暴雨到特大暴雨
                    break;
                case "n26":
                    resourceId = R.drawable.n26; // 未知天气
                    break;
                case "n27":
                    resourceId = R.drawable.n27; // 未知天气
                    break;
                case "n28":
                    resourceId = R.drawable.n28; // 未知天气
                    break;
                case "n29":
                    resourceId = R.drawable.n29; // 未知天气
                    break;
                case "n30":
                    resourceId = R.drawable.n30; // 未知天气
                    break;
                case "n31":
                    resourceId = R.drawable.n31; // 未知天气
                    break;
                case "n53":
                    resourceId = R.drawable.n53; // 未知天气
                    break;
                case "n301":
                    resourceId = R.drawable.n301; // 未知天气
                    break;
                case "n302":
                    resourceId = R.drawable.n302; // 未知天气
                    break;
            }
        }
        return resourceId;
    }

    private void setWeatherIcon(ImageView imageView, String weatherCode) {
        int resourceId = getWeatherIconResId(weatherCode);
        imageView.setImageResource(resourceId);
    }
}

